//
//  StoryTableViewController.m
//  MyItem
//
//  Created by SoulWater on 15/12/30.
//  Copyright © 2015年 admin. All rights reserved.
//

#import "StoryTableViewController.h"
#import "StoryHeadView.h"
#import "StoryCell.h"
#import "StoryModel.h"
#import "DetailStoryTableViewController.h"
@interface StoryTableViewController ()
{
    StoryHeadView *_headView; //头视图
    NSArray *_array;
}

@end

@implementation StoryTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _array = [[NSArray alloc]init];
    
    self.title = @"故事";

    //注册头视图
    [self.tableView registerNib:[UINib nibWithNibName:@"StoryHeadView" bundle:nil] forHeaderFooterViewReuseIdentifier:@"headView"];
    
    //注册单元格
    [self.tableView registerNib:[UINib nibWithNibName:@"StoryCell" bundle:nil] forCellReuseIdentifier:@"storyCell"];
    
    [self loadData];
    
    _headView = [[[NSBundle mainBundle]loadNibNamed:@"StoryHeadView" owner:self options:nil]lastObject];
    
    self.tableView.tableHeaderView = _headView;
    

}

-(void)loadData
{
    NSDictionary *parms = @{
                            @"sign": @"cq_Z3x2C-sY93MEego66Yw",
                            @"d_model": @"iPhone4,1",
                            @"networkType": @"wifi",
                            @"adid": @"5BAD0863-7735-475F-B800-07AF74862C1F",
                            @"st": @"1451460886779",
                            @"body": @"{\"forward\":\"U\",\"networkType\":\"WIFI\",\"refStoryPubDate\":\"1451444737054\",\"refStoryId\":\"1587508\",\"withUpdateCount\":\"true\",\"storyType\":\"firstload\",\"lastNoticeTime\":\"1\",\"messageType\":\"1,2,3,4\",\"num\":\"8\",\"lastReadTime\":\"1451457138565\",\"recommendFlag\":\"\"}",
                            @"osVersion": @"7.1.2",
                            @"d_brand": @"apple",
                            @"client": @"apple",
                            @"screen": @"640*960",
                            @"sv": @"1",
                            @"area": @"15_1213_3038_0",
                            @"build": @"35977",
                            @"uuid": @"hjudwgohxzVu96krv/T6Hg==",
                            @"clientVersion": @"4.4.3",
                            @"openudid": @"a1e609be3483012e09542a204a2a6dfd05c016be",
                            @"partner": @"apple"
                            };

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    [manager POST:@"http://go.m.jd.com/client.action?functionId=getStoryList" parameters:parms success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        
//        NSLog(@"%@",responseObject);
        NSArray *storyInfoList = responseObject[@"storyInfoList"];

    
        NSMutableArray *modelArray = [NSMutableArray array];
        for (NSDictionary *dic in storyInfoList) {
            
            StoryModel *model = [[StoryModel alloc]initWithDataDic:dic];
            
            [modelArray addObject:model];
        }
        
        _dataArray = modelArray;
        
        [self.tableView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
    

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return _dataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    StoryCell *cell = [tableView dequeueReusableCellWithIdentifier:@"storyCell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.storyModel = _dataArray[indexPath.row];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 500;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    DetailStoryTableViewController *detailStory = [[DetailStoryTableViewController alloc]init];
    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:detailStory];

    StoryModel *model = _dataArray[indexPath.row];

    detailStory.storyModel = model;

    
    [self.navigationController presentViewController:nav animated:YES completion:NULL];
   
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
